package com.cssy.yd.logic.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONArray;
import com.cssy.authority.service.IScDepartmentService;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.basic.service.IBasicCompanyService;

import com.cssy.yd.basic.service.IBasicStorageService;

import com.cssy.yd.logic.entity.LogicPoReturnItemBatchDO;
import com.cssy.yd.logic.entity.LogicPoWareItemBatchDO;
import com.cssy.yd.logic.entity.LogicPoWareItemDO;
import com.cssy.yd.logic.entity.LogicSoDO;
import com.cssy.yd.logic.entity.LogicSoItemDO;
import com.cssy.yd.logic.entity.LogicSoWareDO;
import com.cssy.yd.logic.entity.LogicSoWareItemBatchDO;
import com.cssy.yd.logic.entity.LogicSoWareItemDO;
import com.cssy.yd.logic.entity.LogicSoWareItemFreezedealbatchDO;
import com.cssy.yd.logic.entity.LogicSoWarefreezedealDO;
import com.cssy.yd.logic.entity.LogicSoWarefreezedealItemBatchDO;
import com.cssy.yd.logic.entity.LogicSoWarefreezedealItemDO;


import com.cssy.yd.logic.service.ILogicSoItemService;

import com.cssy.yd.logic.service.ILogicSoService;
import com.cssy.yd.logic.service.ILogicSoWareItemService;
import com.cssy.yd.logic.service.ILogicSoWareService;
import com.cssy.yd.logic.service.ILogicSoWarefreezedealService;
import com.cssy.yd.logic.util.SqlUtil;
import com.cssy.yd.logic.util.logic.LogicSoBeanUtil;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

/**
 * 销售出库控制器
 * @author huang.xuming [2013-8-23]
 *
 */
@Controller
@RequestMapping("/yd/logic/soware")
public class SoWareController extends BaseController<LogicSoWareDO> {

	private static final Logger logger = Logger.getLogger(SoWareController.class);
			
	private static final String MANAGER = "/yd/logic/soware/SoWareManager";// 列表页面
	private static final String EIDT = "/yd/logic/soware/SoWareOperation";// 添加、修改、显示页面
	private static final String ITEMMANAGER = "/yd/logic/soware/SoWareItemManager";// 添加、修改、显示页面
	private static final String FITEMMANAGER = "/yd/logic/soware/SoWareFreezedeaManager";// 添加、修改、显示页面
	
	@Resource(name = "logicSoWareService")
	private ILogicSoWareService logicSoWareService;
	@Resource(name = "logicSoService")
	private ILogicSoService logicSoService;
	@Resource(name = "logicSoItemService")
	private ILogicSoItemService logicSoItemService;
	
	@Resource(name = "logicSoWarefreezedealService")
	private ILogicSoWarefreezedealService logicSoWarefreezedealService;
	
	@Resource(name = "logicSoWareItemService")
	private ILogicSoWareItemService logicSoWareItemService;
	@Resource(name = "basicCompanyService")
	IBasicCompanyService basicCompanyService;
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Resource(name = "basicwhService")
	private IBasicStorageService basicwhService;

	@Resource(name = "scDepartmentService")
    private IScDepartmentService scDepartmentService;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}
	
	@RequestMapping("itemManager")
	public String itemManager(HttpServletRequest request, Model model)
			throws Exception {
		String itemdata =request.getParameter("data");
		String billstatus =request.getParameter("billstatus");
		String whcode =request.getParameter("whcode");
		LogicSoWareItemDO soWareItemDO =JSONArray.parseObject(new String(itemdata.getBytes("ISO-8859-1"),"UTF-8"), LogicSoWareItemDO.class);
		LogicSoWareItemBatchDO wareItemBatchDO = new LogicSoWareItemBatchDO();
		if(soWareItemDO != null){
			wareItemBatchDO.setMaterialsid(soWareItemDO.getMaterialsid());
			wareItemBatchDO.setProdNo(soWareItemDO.getProdNo());
			wareItemBatchDO.setProdName(soWareItemDO.getProdName());
			//wareItemBatchDO.setProdBatch(soWareItemDO.getProdBatch());
			wareItemBatchDO.setProdSpecDesc(soWareItemDO.getProdSpecDesc());
			wareItemBatchDO.setProdBUnit(soWareItemDO.getProdBUnit());
			wareItemBatchDO.setProdUnit(soWareItemDO.getProdUnit());
			wareItemBatchDO.setProdUnitConvert(soWareItemDO.getProdUnitConvert());
//			wareItemBatchDO.setProdBSfquantity(prodBSfquantity);
//			wareItemBatchDO.setProdSfquantity(prodSfquantity);
			wareItemBatchDO.setKyBQuantity(soWareItemDO.getKyBQuantity());
			wareItemBatchDO.setKyQuantity(soWareItemDO.getKyQuantity());
			wareItemBatchDO.setTmpCanCkQuantity(soWareItemDO.getTmpCanCkQuantity());
			wareItemBatchDO.setStatus(soWareItemDO.getStatus());
			model.addAttribute("wareItemBatchDO",JSONArray.toJSONString(wareItemBatchDO));
			model.addAttribute("itemBatch",soWareItemDO.getItemBatch());
		}
		model.addAttribute("billstatus",billstatus);
		model.addAttribute("whcode",whcode);
		return ITEMMANAGER;
	}

	@RequestMapping("fzdItemManager")
	public String fzdItemManager(HttpServletRequest request, Model model)
			throws Exception {
		String itemdata =request.getParameter("data");
		String billstatus =request.getParameter("billstatus");
		String updstatus =request.getParameter("updstatus");
//		String whcode =request.getParameter("whcode");
		if(itemdata !=null && !"".equals(itemdata.trim())){
		LogicSoWareItemDO soWareItemDO =JSONArray.parseObject(new String(itemdata.getBytes("ISO-8859-1"),"UTF-8"), LogicSoWareItemDO.class);
//		LogicSoWareItemBatchDO wareItemBatchDO = new LogicSoWareItemBatchDO();
		if(soWareItemDO != null){
//			wareItemBatchDO.setMaterialsid(soWareItemDO.getMaterialsid());
//			wareItemBatchDO.setProdNo(soWareItemDO.getProdNo());
//			wareItemBatchDO.setProdName(soWareItemDO.getProdName());
//			//wareItemBatchDO.setProdBatch(soWareItemDO.getProdBatch());
//			wareItemBatchDO.setProdSpecDesc(soWareItemDO.getProdSpecDesc());
//			wareItemBatchDO.setProdBUnit(soWareItemDO.getProdBUnit());
//			wareItemBatchDO.setProdUnit(soWareItemDO.getProdUnit());
//			wareItemBatchDO.setProdUnitConvert(soWareItemDO.getProdUnitConvert());
////			wareItemBatchDO.setProdBSfquantity(prodBSfquantity);
////			wareItemBatchDO.setProdSfquantity(prodSfquantity);
//			wareItemBatchDO.setKyBQuantity(soWareItemDO.getKyBQuantity());
//			wareItemBatchDO.setKyQuantity(soWareItemDO.getKyQuantity());
//			wareItemBatchDO.setTmpCanCkQuantity(soWareItemDO.getTmpCanCkQuantity());
//			wareItemBatchDO.setStatus(soWareItemDO.getStatus());
//			model.addAttribute("wareItemBatchDO",JSONArray.toJSONString(wareItemBatchDO));
			model.addAttribute("itemBatch",soWareItemDO.getItemBatch());
		}
		}
		model.addAttribute("updstatus", updstatus);
		model.addAttribute("billstatus",billstatus);
//		model.addAttribute("whcode",whcode);
		return FITEMMANAGER;
	}
	
	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		SearchResult result = logicSoWareService.findListByExample(scs);
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	@RequestMapping(value = "/itemlist")
	public void itemlist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String itembatch =request.getParameter("itembatch");
		List<LogicSoWareItemBatchDO> itemBatchDOs=null;
		if(itembatch != null  && !"".equals(itembatch)){
			itemBatchDOs=JSONArray.parseArray(new String(itembatch.getBytes("ISO-8859-1"),"UTF-8"), LogicSoWareItemBatchDO.class);
		}
		SearchResult result= new SearchResult();
		if(itemBatchDOs != null && itemBatchDOs.size() > 0){
			result.setRecordCount(itemBatchDOs.size());
			result.setResults(itemBatchDOs);
		}
		 OutJsonGrid out=new OutJsonGrid(result);
		 out.printJSon(response);
		
	}
	@RequestMapping(value = "/pzdItemlist")
	public void pzdItemlist(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String itembatch =request.getParameter("itembatch");
		String updstatus = request.getParameter("updstatus");
		SearchResult result= new SearchResult();
		List<LogicSoWareItemFreezedealbatchDO> batchDOs = new ArrayList<LogicSoWareItemFreezedealbatchDO>();
		if("1".equals(updstatus)){
			if(itembatch != null  && !"".equals(itembatch)){
				batchDOs=JSONArray.parseArray(new String(itembatch.getBytes("ISO-8859-1"),"UTF-8"), LogicSoWareItemFreezedealbatchDO.class);
			}
		}else{
			List<LogicSoWarefreezedealItemBatchDO> itemBatchDOs=null;
			if(itembatch != null  && !"".equals(itembatch)){
				itemBatchDOs=JSONArray.parseArray(new String(itembatch.getBytes("ISO-8859-1"),"UTF-8"), LogicSoWarefreezedealItemBatchDO.class);
			}
			LogicSoWareItemFreezedealbatchDO lFreezedealbatchDO = null;
			if(itemBatchDOs != null && itemBatchDOs.size() > 0){
				for (LogicSoWarefreezedealItemBatchDO warefreezedealItemBatchDO : itemBatchDOs) {
					if((warefreezedealItemBatchDO.getCkQuantity()==null?0.0:warefreezedealItemBatchDO.getCkQuantity()) < (warefreezedealItemBatchDO.getDjQuantity()==null?0.0:warefreezedealItemBatchDO.getDjQuantity())){
						lFreezedealbatchDO = new LogicSoWareItemFreezedealbatchDO();
						lFreezedealbatchDO.setMaterialsid(warefreezedealItemBatchDO.getMaterialsid());
						lFreezedealbatchDO.setProdNo(warefreezedealItemBatchDO.getProdNo());
						lFreezedealbatchDO.setProdName(warefreezedealItemBatchDO.getProdName());
						lFreezedealbatchDO.setProdSpecDesc(warefreezedealItemBatchDO.getProdSpecDesc());
						lFreezedealbatchDO.setProdBatch(warefreezedealItemBatchDO.getProdBatch());
						lFreezedealbatchDO.setStatus(warefreezedealItemBatchDO.getStatus());
						lFreezedealbatchDO.setProdUnit(warefreezedealItemBatchDO.getProdUnit());
						lFreezedealbatchDO.setProdUnitConvert(warefreezedealItemBatchDO.getProdUnitConvert());
						lFreezedealbatchDO.setProdBUnit(warefreezedealItemBatchDO.getProdBUnit());
						lFreezedealbatchDO.setFrozenQuantity(warefreezedealItemBatchDO.getDjQuantity());
						lFreezedealbatchDO.setFrozenBQuantity(warefreezedealItemBatchDO.getDjBQuantity());
						lFreezedealbatchDO.setCkBQuantity(warefreezedealItemBatchDO.getCkBQuantity());
						lFreezedealbatchDO.setCkQuantity(warefreezedealItemBatchDO.getCkQuantity());
						lFreezedealbatchDO.setDjjdBQuantity(warefreezedealItemBatchDO.getDjBQuantity());
						lFreezedealbatchDO.setDjjdQuantity(warefreezedealItemBatchDO.getDjjdQuantity());
//						lFreezedealbatchDO.setKyBQuantity(kyBQuantity);
//						lFreezedealbatchDO.setKyQuantity(kyQuantity);
						batchDOs.add(lFreezedealbatchDO);
					}
				}
			}
		}
		result.setRecordCount(batchDOs.size());
		result.setResults(batchDOs);

		 OutJsonGrid out=new OutJsonGrid(result);
		 out.printJSon(response);
		
	}
	/**
	 * 获取销售出库单相应的冻结未出库列表
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/listFreeze")
	public void listFreeze(HttpServletRequest request, HttpServletResponse response){
		//TODO
	}

	@Override
	public String operationForm(HttpServletRequest request, 
			@PathVariable String id,@PathVariable String flag, Model model) throws Exception {
		
		
		if (OperationFlag.ADD.equals(flag)) {// 我新增加时操作
			model.addAttribute("flag", "ADD");
			
		} else {
			LogicSoWareDO soware = logicSoWareService.findListById(id);
			/*List<LogicSoWareItemDO> sowareitem = logicSoWareItemService.findItemListBysowareId(soware.getId());
			List<LogicSoWareItemDO> sowareitem1 = new ArrayList<LogicSoWareItemDO>();
			List<LogicSoWareItemDO> sowareitem2 = new ArrayList<LogicSoWareItemDO>();
			for(LogicSoWareItemDO swItem : sowareitem){
				
				if(1==swItem.getSourceBillType()){
					sowareitem1.add(swItem);
				}else if(2==swItem.getSourceBillType()){
					sowareitem2.add(swItem);
				}else{
					
				}
			}*/
			
			model.addAttribute("flag", "EDIT");
			model.addAttribute("soware", soware);
			//model.addAttribute("sowareitem", sowareitem);
			/*model.addAttribute("sowareitem1", sowareitem1);
			model.addAttribute("sowareitem2", sowareitem2);*/
			SearchConditions sc = new SearchConditions();
			sc.setRemovedProperty(null);// 判断是否有删除标识
			sc.addCondition("tid", soware.getId(), SearchCondition.OP_EQUAL);
		}
		
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
//		List<BasicStorageinfoDO> storageinfolist = new ArrayList<BasicStorageinfoDO>();
		
		//获取合法销售订单
		SearchConditions scsso = new SearchConditions();
		scsso.addCondition("billstatus", "4,5", SearchCondition.OP_IN);//订单状态-二审完结、三审完结
		scsso.addCondition("ware_status", "0,1", SearchCondition.OP_IN);//出库状态-非完全出库
		scsso.setLimitPage(false);//不分页
		@SuppressWarnings("unchecked")
		List<LogicSoDO> solist = logicSoService.findListByExample(scsso).getResults();
		model.addAttribute("solist", solist);
		
		//客户信息
		SearchConditions scs = new SearchConditions();
		JqGridHandler hander = new JqGridHandler(request);
		hander.getWheres(scs);
		model.addAttribute("companylist", basicCompanyService.find(scs));
		
		//部门列表
		model.addAttribute("depts",SoController.getDepartmentList(scDepartmentService));
				
		return EIDT;
	}
	
	@RequestMapping("/griditems")
	public void griditems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String type = request.getParameter("type");
		if(StringUtils.isNotBlank(id)){
			LogicSoWareDO soware = logicSoWareService.findListById(id);
			List<LogicSoWareItemDO> sowareitem = logicSoWareItemService.findItemListBysowareId(soware.getId());
			
			if("grid1".equals(type)){
				
				List<LogicSoWareItemDO> _sowareitem = new ArrayList<LogicSoWareItemDO>();
				
				for(LogicSoWareItemDO swItem : sowareitem){
					if(swItem==null){
						continue;
					}
					if(Integer.valueOf("1").equals(swItem.getSourceBillType())){
						_sowareitem.add(swItem);
					}
				}
				
				OutJsonGrid out = new OutJsonGrid(_sowareitem);
				out.printJSon(response);
			}
			
			if("grid2".equals(type)){
				List<LogicSoWareItemDO> _sowareitem = new ArrayList<LogicSoWareItemDO>();
				
				for(LogicSoWareItemDO swItem : sowareitem){
					if(swItem==null){
						continue;
					}
					if(Integer.valueOf("2").equals(swItem.getSourceBillType())){
						_sowareitem.add(swItem);
					}
					
				}
				
				OutJsonGrid out = new OutJsonGrid(_sowareitem);
				out.printJSon(response);
			}
		}
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicSoWareDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		String itemdata =request.getParameter("itemdata");
		if(itemdata==null&&"".equals(itemdata)){
			bodyMap.isFail();
			bodyMap.configWebInfor("物料内容不能为空！");
			return bodyMap.modelMap;
		}else{
			List<LogicSoWareItemDO> soWareItemDO  = JSONArray.parseArray(itemdata, LogicSoWareItemDO.class);
			for (LogicSoWareItemDO sItemDO : soWareItemDO) {
				if("Y".equals(sItemDO.getOpenBatch())){
					
				
				if(sItemDO.getItemBatch()==null ||  "".equals(sItemDO.getItemBatch())){
					bodyMap.isFail();
					bodyMap.configWebInfor("物料编码为:'"+sItemDO.getProdNo()+"'没有分批次填写数据！");
					return bodyMap.modelMap;
				}else{
					List<LogicSoWareItemBatchDO> batchDOs  = JSONArray.parseArray(sItemDO.getItemBatch(), LogicSoWareItemBatchDO.class);
					if(batchDOs==null || batchDOs.size() <= 0){
						bodyMap.isFail();
						bodyMap.configWebInfor("物料编码为:'"+sItemDO.getProdNo()+"'没有分批次填写数据！");
						return bodyMap.modelMap;
					}
				}
				}
			}
		}try {
			logicSoWareService.soWareSave(request, curAP);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
			bodyMap.configWebId(curAP.getId());
		} catch (Exception e) {
			logger.warn(e.getMessage(),e);
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				LogicSoWareDO ld=logicSoWareService.findListById(field);
				if(ld!=null){
					ld.setRemoved(1);
					logicSoWareService.update(ld, SessionUtil.getUser(request));
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			logger.warn(e.getMessage(),e);
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}
	
	/**
	 * 返回soitem的表格json
	 * sowareOperation.jsp、sowarefreezeOperation.jsp中使用到
	 * @param soNo
	 * @param request
	 * @param response
	 */
	@RequestMapping("/soitemlist")
	public void soitemlist(@RequestParam("soNo") String soNo, HttpServletRequest request, HttpServletResponse response){
		BodyMsg bodyMap = BodyMsg.getInstance();
		
		SearchResult result= new SearchResult();
		LogicSoItemDO bean = new LogicSoItemDO();
		
		List<LogicSoItemDO> soitemlist = new ArrayList<LogicSoItemDO>();
		List<LogicSoWareItemDO> swiList = new ArrayList<LogicSoWareItemDO>();
		if(StringUtils.isNotBlank(soNo)){
			bean.setSoNo(soNo);
			soitemlist = logicSoItemService.findListByDO(bean);
			if(null != soitemlist){
				for(LogicSoItemDO soitem: soitemlist){
					LogicSoWareItemDO lsi = new LogicSoWareItemDO();
					lsi.setProdBSfquantity(null);
					lsi.setMaterialsid(soitem.getMaterialsid());
					lsi.setCliName(soitem.getCliName());
					lsi.setCliNo(soitem.getCliNo());
					lsi.setProdBatch(soitem.getProdBatch());
					lsi.setProdBQuantity(soitem.getProdBQuantity());
					lsi.setProdBUnit(soitem.getProdBUnit());
					lsi.setProdModel(soitem.getProdModel());
					lsi.setProdName(soitem.getProdName());
					lsi.setProdNo(soitem.getProdNo());
					lsi.setProdPrice(soitem.getProdBUnitPrice());
					lsi.setProdPriceWithTax(soitem.getProdBUnitPriceWithTax());
					lsi.setProdQuantity(soitem.getProdQuantity());
					lsi.setProdSpecDesc(soitem.getProdSpecDesc());
					lsi.setProdUnit(soitem.getProdUnit());
					lsi.setProdUnitConvert(soitem.getProdUnitConvert());
					lsi.setFrozenBQuantity(soitem.getFrozenBQuantity());
					lsi.setFrozenQuantity(soitem.getFrozenQuantity());
					lsi.setStatus(soitem.getStatus());
					lsi.setCkBQuantity(soitem.getCkBQuantity());
					lsi.setCkQuantity(soitem.getCkQuantity());
					lsi.setYtQuantity(0d);
					lsi.setYtBQuantity(0d);
					lsi.setSourceBillType(1);
					lsi.setOpenBatch(soitem.getOpenBatch());
//					lsi.setKyBQuantity(kyBQuantity)
//					lsi.setKyQuantity(kyQuantity)
					
					swiList.add(lsi);
				}
			}
		}else{
			String msg = "传入的soNo参数为空！";
			logger.warn(msg);
			bodyMap.isFail(GlobalResLoader.getOpfailed() + msg);
		}
		
		result.setResults(swiList);
		result.setRecordCount(swiList.size());
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	/**
	 * 返回soitem的表格json
	 * sowareOperation.jsp中使用到
	 * @param soNo
	 * @param request
	 * @param response
	 */
	@RequestMapping("/sofreezeitemlist")
	public void sofreezeitemlist(@RequestParam("soNo") String soNo, HttpServletRequest request, HttpServletResponse response){
		//TODO 
		
		BodyMsg bodyMap = BodyMsg.getInstance();
		
		SearchResult result= new SearchResult();
		
		List<LogicSoWareItemDO> swiList = new ArrayList<LogicSoWareItemDO>();
		if(StringUtils.isNotBlank(soNo)){
			
			SearchConditions scs = new SearchConditions();
			scs.addCondition("so_no", soNo, SearchCondition.OP_EQUAL);
			scs.addCondition("billstatus", 2, SearchCondition.OP_EQUAL);
			List<LogicSoWarefreezedealDO> swfdList = logicSoWarefreezedealService.findListByExample(scs).getResults();
			
			
			
			if(null != swfdList){
				for(LogicSoWarefreezedealDO curSwfd : swfdList){
					LogicSoItemDO logicSoItemDO = new LogicSoItemDO();
					logicSoItemDO.setSoNo(curSwfd.getSoNo());
					List<LogicSoItemDO> tmpSoItem = logicSoItemService.findListByDO(logicSoItemDO);
					List<LogicSoWarefreezedealItemDO> swfdItemList = logicSoWarefreezedealService.findItemListBysowfdId(curSwfd.getId());
					for(LogicSoWarefreezedealItemDO soitem : swfdItemList){
						if(soitem.getDjBQuantity()-soitem.getDjckBQuantity() - soitem.getDjjdBQuantity() > 0){
							LogicSoWareItemDO lsi = new LogicSoWareItemDO();
							lsi.setMaterialsid(soitem.getMaterialsid());
							lsi.setCliName(soitem.getCliName());
							lsi.setCliNo(soitem.getCliNo());
							lsi.setProdBatch(soitem.getProdBatch());
							lsi.setProdBQuantity(soitem.getProdBQuantity());
							lsi.setProdBUnit(soitem.getProdBUnit());
							lsi.setProdModel(soitem.getProdModel());
							lsi.setProdName(soitem.getProdName());
							lsi.setProdNo(soitem.getProdNo());
							lsi.setProdPrice(soitem.getProdPrice()); //基本单位含税价
							//lsi.setProdPriceWithTax(soitem.getProdPriceWithTax());
							lsi.setProdQuantity(soitem.getProdQuantity());
							lsi.setProdSpecDesc(soitem.getProdSpecDesc());
							lsi.setProdUnit(soitem.getProdUnit());
							lsi.setProdUnitConvert(soitem.getProdUnitConvert());
							//本单据此物料可冻结数量
							lsi.setFrozenBQuantity(soitem.getDjBQuantity()-soitem.getDjckBQuantity()-soitem.getDjjdBQuantity());
							lsi.setFrozenQuantity(soitem.getDjQuantity()-soitem.getDjckQuantity()-soitem.getDjjdQuantity());
							
							lsi.setStatus(soitem.getStatus());
							lsi.setCkBQuantity(soitem.getCkBQuantity());
							lsi.setCkQuantity(soitem.getCkQuantity());
							lsi.setYtQuantity(0d);
							lsi.setYtBQuantity(0d);
							lsi.setSourceBillType(2);
							lsi.setSourceBillNo(curSwfd.getNo());
							lsi.setItemBatch(soitem.getItemBatch());
							lsi.setOpenBatch(soitem.getOpenBatch());
//							lsi.setKyBQuantity(soitem.getKyBQuantity());
//							lsi.setKyQuantity(soitem.getKyQuantity());
							
							//销售订单的赋值给冻结处理单
							for(LogicSoItemDO si :tmpSoItem){
								if(LogicSoBeanUtil.getInstance().isSameMaterial(si, lsi)){
									lsi.setProdPrice(si.getProdPrice());
									lsi.setProdPriceWithTax(si.getProdPriceWithTax());
								}
							}
							
							lsi.setWhcode(curSwfd.getWhcode());
							lsi.setWhname(curSwfd.getWhname());
							
							swiList.add(lsi);
						}
						
					}
				}
			}
			
			
		}else{
			String msg = "传入的soNo参数为空！";
			logger.warn(msg);
			bodyMap.isFail(GlobalResLoader.getOpfailed() + msg);
		}
		
		result.setResults(swiList);
		result.setRecordCount(swiList.size());
		OutJsonGrid out = new OutJsonGrid(result);
		out.printJSon(response);
	}
	
	@RequestMapping(value = "/exportExcel")
	public void printer(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		try {
			
			String id = request.getParameter("id");
			LogicSoWareDO soware=new LogicSoWareDO();
			List<LogicSoWareItemDO> lp=new ArrayList<LogicSoWareItemDO>();
			List<LogicSoWareItemDO> sowareitem1 = new ArrayList<LogicSoWareItemDO>();
			List<LogicSoWareItemDO> sowareitem2 = new ArrayList<LogicSoWareItemDO>();
			if(!"".equals(id)&&id!=null){
				soware = logicSoWareService.findListById(id);
			}
			if(soware.getId()!=null){
				lp = logicSoWareItemService.findItemListBysowareId(soware.getId());
			}
			if(lp.size()>0){
				for(LogicSoWareItemDO swItem : lp){
					if(1==swItem.getSourceBillType()){
						sowareitem1.add(swItem);
					}else if(2==swItem.getSourceBillType()){
						sowareitem2.add(swItem);
					}else{
						
					}
				}
				request.setAttribute("soware", soware);
				request.setAttribute("lp", lp);
				request.setAttribute("sowareitem1", sowareitem1);
				request.setAttribute("sowareitem2", sowareitem2);
				
				response.setContentType("application/vnd.ms-excel");
				response.setHeader("Content-disposition", "attachment;filename=" + soware.getNo() + ".xls");
			
				RequestDispatcher dispatcher = request.getRequestDispatcher("/views/printer/SoWarePrint.jsp");
			
				dispatcher.forward(request, response);
			}
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
//	@RequestMapping(value = "/exportExcel")
//	public void printer(HttpServletRequest request, HttpServletResponse response) throws Exception{
		//TODO 导出功能
//		try {
//			
//			
//			String id = request.getParameter("id");
//			LogicSoWareDO soware=new LogicSoWareDO();
//			List<LogicSoWareItemDO> lp=new ArrayList<LogicSoWareItemDO>();
//			List<LogicFreightageDO> freightageDOs=new ArrayList<LogicFreightageDO>();
//			if(!"".equals(id)&&id!=null){
//				soware = logicSoWareService.findListById(id);
//			}
//			if(soware.getId()!=null){
//				lp = logicSoWareItemService.findItemListBysowareId(soware.getId());
//				SearchConditions sc = new SearchConditions();
//				sc.setRemovedProperty(null);//判断是否有删除标识
//				sc.addCondition("tid", soware.getId(), SearchCondition.OP_EQUAL);
//				freightageDOs=logicFreightageService.findListByExample(sc).getResults();
//			}
//			if(lp.size()>0){
//				request.setAttribute("soware", soware);
//				request.setAttribute("lp", lp);
//				request.setAttribute("row", lp.size());
//				if(freightageDOs.size()>0){
//					for(int i=0;i<freightageDOs.size();i++){
//						if("铁运费".equals(freightageDOs.get(i).getFreightagename())){
//							request.setAttribute("T", freightageDOs.get(i).getFreightageprice());
//						}
//						if("海运费".equals(freightageDOs.get(i).getFreightagename())){
//							request.setAttribute("H", freightageDOs.get(i).getFreightageprice());
//						}
//						if("短运费".equals(freightageDOs.get(i).getFreightagename())){
//							request.setAttribute("D", freightageDOs.get(i).getFreightageprice());
//						}
//						if("装卸费".equals(freightageDOs.get(i).getFreightagename())){
//							request.setAttribute("Z", freightageDOs.get(i).getFreightageprice());
//						}
//						if("落箱费".equals(freightageDOs.get(i).getFreightagename())){
//							request.setAttribute("L", freightageDOs.get(i).getFreightageprice());
//						}
//						if("其他".equals(freightageDOs.get(i).getFreightagename())){
//							request.setAttribute("Q", freightageDOs.get(i).getFreightageprice());
//						}
//					}
//					
//				}
//				
//				response.setContentType("application/vnd.ms-excel");
//				response.setHeader("Content-disposition", "attachment;filename=export_" + System.currentTimeMillis() + ".xls");
//			
//				RequestDispatcher dispatcher = request.getRequestDispatcher("/views/printer/SoWarePrint.jsp");
//			
//				dispatcher.forward(request, response);
//			}
//		} catch (ServletException e) {
//			e.printStackTrace();
//		}
//	}
	
//	@RequestMapping(value = "/deliverprinter")
//	public void deliverprinter(HttpServletRequest request, HttpServletResponse response) throws Exception{
//
//		try {
//			
//			
//			String id = request.getParameter("id");
//			LogicSoWareDO soware=new LogicSoWareDO();
//			List<LogicSoWareItemDO> lp=new ArrayList<LogicSoWareItemDO>();
//			if(!"".equals(id)&&id!=null){
//				soware = logicSoWareService.findListById(id);
//			}
//			if(soware.getId()!=null){
//				lp = logicSoWareItemService.findItemListBysowareId(soware.getId());
//			}
//			if(lp.size()>0){
//				request.setAttribute("soware", soware);
//				request.setAttribute("lp", lp);
//				request.setAttribute("row", lp.size());
//				response.setContentType("application/vnd.ms-excel");
//				response.setHeader("Content-disposition", "attachment;filename=export_" + System.currentTimeMillis() + ".xls");
//			
//				RequestDispatcher dispatcher = request.getRequestDispatcher("/views/printer/DeliverPrint.jsp");
//			
//				dispatcher.forward(request, response);
//			}
//		} catch (ServletException e) {
//			e.printStackTrace();
//		}
//	}
}
